package com.xinqi.modules.chat.chat.group.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xinqi.common.base.user.Account;
import com.xinqi.common.core.constant.MQConstants;
import com.xinqi.common.core.enums.RocketMqEnum;
import com.xinqi.common.core.enums.RocketMqRongyunEnum;
import com.xinqi.common.rocketmq.dto.ChatMessageMessageBody;
import com.xinqi.common.rocketmq.dto.MqMessageDTO;
import com.xinqi.common.rocketmq.event.ProducerEvent;
import com.xinqi.modules.chat.chat.group.domain.ChatGroupEntity;
import com.xinqi.modules.chat.chat.group.domain.ChatGroupUserEntity;
import com.xinqi.modules.chat.chat.group.dto.rsp.ChatGroupUserResultDTO;
import com.xinqi.modules.chat.chat.group.message.GroupNoticeMessage;
import com.xinqi.modules.chat.enums.group.GroupNoticeOperationEnum;
import com.xinqi.modules.chat.enums.group.GroupUserLeaderFlagEnum;
import com.xinqi.modules.user.user.dto.rsp.UserResultDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @version V1.0
 * @Title: 群聊融云通知消息
 * @ClassName: com.xinqi.modules.chat.chat.group.service.impl.ChatGroupMessageService.java
 * @Description:
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 * @author: yyzhen
 * @date: 2023/7/26 15:26
 */
@Log4j2
@Validated
@Service("chatGroupMessageService")
@RequiredArgsConstructor
public class ChatGroupMessageService {

    /**
     * 创建群组
     *
     * @param user
     * @param group
     * @return
     */
    @Async("threadPoolTaskExecutor")
    public void sendCreateMessage(Account user, ChatGroupEntity group) {
        if (Objects.isNull(user)) {
            return;
        }
        if (Objects.isNull(group)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.CREATE_OPERATION.getCode());
        noticeMessage.setMessage("“" + user.getNickname() + "”创建群聊“" + group.getGroupName() + "”");
        noticeMessage.setGroupId(group.getGroupId());

        Map<String, String> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetGroupName", group.getGroupName());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{group.getGroupId()});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 修改群名称
     *
     * @param user
     * @param group
     * @return
     */
    @Async("threadPoolTaskExecutor")
    public void sendRenameMessage(Account user, ChatGroupEntity group) {
        if (Objects.isNull(user)) {
            return;
        }
        if (Objects.isNull(group)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.RENAME_OPERATION.getCode());
        noticeMessage.setMessage("“" + user.getNickname() + "”修改群聊名称为“" + group.getGroupName() + "”");
        noticeMessage.setGroupId(group.getGroupId());

        Map<String, String> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetGroupName", group.getGroupName());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{group.getGroupId()});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 添加群成员
     *
     * @param user
     * @param userResultList
     * @param groupId
     * @return
     */
    @Async("threadPoolTaskExecutor")
    public void sendAddMessage(Account user, List<UserResultDTO> userResultList, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (CollectionUtil.isEmpty(userResultList)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.ADD_OPERATION.getCode());
        noticeMessage.setMessage("添加群成员");
        noticeMessage.setGroupId(groupId);

        List<String> userId = new ArrayList<>();
        List<String> nickname = new ArrayList<>();
        userResultList.stream().forEach(item -> {
            userId.add(item.getId().toString());
            nickname.add(item.getNickname());
        });
        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetUserIds", userId.toArray(new String[userId.size()]));
        data.put("targetUserDisplayNames", nickname.toArray(new String[nickname.size()]));
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 主动加入群聊
     *
     * @param user
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendAddMessage(Account user, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }

        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.ADD_OPERATION.getCode());
        noticeMessage.setMessage("添加群成员");
        noticeMessage.setGroupId(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetUserIds", new String[]{user.getId().toString()});
        data.put("targetUserDisplayNames", new String[]{user.getNickname()});
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 加入班级群
     *
     * @param user
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendAddMessage(UserResultDTO user, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.ADD_OPERATION.getCode());
        noticeMessage.setMessage("添加群成员");
        noticeMessage.setGroupId(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetUserIds", new String[]{user.getId().toString()});
        data.put("targetUserDisplayNames", new String[]{user.getNickname()});
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 移出群成员
     *
     * @param user
     * @param userResultList
     * @param groupId
     * @return
     */
    @Async("threadPoolTaskExecutor")
    public void sendKickedMessage(Account user, List<UserResultDTO> userResultList, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (CollectionUtil.isEmpty(userResultList)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.KICKED_OPERATION.getCode());
        noticeMessage.setMessage("移出群成员");
        noticeMessage.setGroupId(groupId);

        List<String> userId = new ArrayList<>();
        List<String> nickname = new ArrayList<>();
        userResultList.stream().forEach(item -> {
            userId.add(item.getId().toString());
            nickname.add(item.getNickname());
        });
        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetUserIds", userId.toArray(new String[userId.size()]));
        data.put("targetUserDisplayNames", nickname.toArray(new String[nickname.size()]));
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 退出群组
     *
     * @param user
     * @param groupId
     * @param newCreatorId 如退出群的用户为群创建者，则 newCreatorId 为新的群创建者 ID ，否则为 nul
     * @return
     */
    @Async("threadPoolTaskExecutor")
    public void sendQuitMessage(UserResultDTO user, String groupId, String newCreatorId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.QUIT_OPERATION.getCode());
        noticeMessage.setMessage("退出群聊");

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetUserIds", new String[]{user.getId().toString()});
        data.put("targetUserDisplayNames", new String[]{user.getNickname()});
        data.put("newCreatorId", newCreatorId);
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 转让群主
     *
     * @param mainUser
     * @param shiftUser
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendShiftLeaderMessage(ChatGroupUserResultDTO mainUser, ChatGroupUserResultDTO shiftUser, String groupId) {
        if (Objects.isNull(mainUser)) {
            return;
        }
        if (Objects.isNull(shiftUser)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(mainUser.getUserId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.SHIFT_LEADER.getCode());
        noticeMessage.setMessage("转让群主");
        noticeMessage.setGroupId(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", mainUser.getNickname());
        data.put("targetUserIds", new String[]{shiftUser.getUserId().toString()});
        data.put("targetUserDisplayNames", new String[]{shiftUser.getNickname()});
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(mainUser.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--移除群管理
     *
     * @param user
     * @param userResultList
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendRemoveManageMessage(Account user, List<UserResultDTO> userResultList, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (CollectionUtil.isEmpty(userResultList)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.REMOVE_MANAGE.getCode());
        noticeMessage.setMessage("移除群管理");
        noticeMessage.setGroupId(groupId);

        List<String> userId = new ArrayList<>();
        List<String> nickname = new ArrayList<>();
        userResultList.stream().forEach(item -> {
            userId.add(item.getId().toString());
            nickname.add(item.getNickname());
        });
        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetUserIds", userId.toArray(new String[userId.size()]));
        data.put("targetUserDisplayNames", nickname.toArray(new String[nickname.size()]));
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--添加群管理
     *
     * @param user
     * @param userResultList
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendAddManageMessage(Account user, List<UserResultDTO> userResultList, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (CollectionUtil.isEmpty(userResultList)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.ADD_MANAGE.getCode());
        noticeMessage.setMessage("添加群管理");
        noticeMessage.setGroupId(groupId);

        List<String> userId = new ArrayList<>();
        List<String> nickname = new ArrayList<>();
        userResultList.stream().forEach(item -> {
            userId.add(item.getId().toString());
            nickname.add(item.getNickname());
        });
        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("targetUserIds", userId.toArray(new String[userId.size()]));
        data.put("targetUserDisplayNames", nickname.toArray(new String[nickname.size()]));
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 解散群组
     *
     * @param user
     * @param groupId
     * @return
     */
    @Async("threadPoolTaskExecutor")
    public void sendDismissMessage(Account user, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.DISMISS_OPERATION.getCode());
        noticeMessage.setMessage("解散群聊");
        noticeMessage.setGroupId(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 解散群组
     *
     * @param user
     * @param group
     */
    @Async("threadPoolTaskExecutor")
    public void sendDismissMessage(UserResultDTO user, ChatGroupEntity group) {
        if (Objects.isNull(group)) {
            return;
        }
        if (Objects.isNull(user)) {
            user = new UserResultDTO();
            user.setId(group.getUserId());
            user.setNickname("老师");
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.DISMISS_OPERATION.getCode());
        noticeMessage.setMessage("解散群聊");
        noticeMessage.setGroupId(group.getGroupId());

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{group.getGroupId()});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 群组禁言
     *
     * @param user
     * @param groupUser
     */
    @Async("threadPoolTaskExecutor")
    public void sendProhibitionMessage(Account user, ChatGroupUserEntity groupUser) {
        if (Objects.isNull(user)) {
            return;
        }
        if (Objects.isNull(groupUser)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.PROHIBITION_OPERATION.getCode());
        noticeMessage.setGroupId(groupUser.getGroupId());
        if (Objects.equals(GroupUserLeaderFlagEnum.ONE.getCode(), groupUser.getLeaderFlag())) {
            noticeMessage.setMessage("群主“" + user.getNickname() + "”开启了全员禁言");
        } else if (Objects.equals(GroupUserLeaderFlagEnum.TWO.getCode(), groupUser.getLeaderFlag())) {
            noticeMessage.setMessage("管理员“" + user.getNickname() + "”开启了全员禁言");
        } else {
            return;
        }
        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupUser.getGroupId()});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupUser.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 解除群组禁言
     *
     * @param user
     * @param groupUser
     */
    @Async("threadPoolTaskExecutor")
    public void sendCancelProhibitionMessage(Account user, ChatGroupUserEntity groupUser) {
        if (Objects.isNull(user)) {
            return;
        }
        if (Objects.isNull(groupUser)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.CANCEL_PROHIBITION_OPERATION.getCode());
        if (Objects.equals(GroupUserLeaderFlagEnum.ONE.getCode(), groupUser.getLeaderFlag())) {
            noticeMessage.setMessage("群主“" + user.getNickname() + "”解除了全员禁言");
        } else if (Objects.equals(GroupUserLeaderFlagEnum.TWO.getCode(), groupUser.getLeaderFlag())) {
            noticeMessage.setMessage("管理员“" + user.getNickname() + "”解除了全员禁言");
        } else {
            return;
        }
        noticeMessage.setGroupId(groupUser.getGroupId());

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupUser.getGroupId()});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupUser.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 消息置顶
     *
     * @param user
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendTopMessage(Account user, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.TOP_OPERATION.getCode());
        noticeMessage.setMessage("“" + user.getNickname() + "”置顶了一条消息");
        noticeMessage.setGroupId(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 取消消息置顶
     *
     * @param user
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendCancelTopMessage(Account user, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.CANCEL_TOP_OPERATION.getCode());
        noticeMessage.setMessage("“" + user.getNickname() + "”移除了一条置顶消息");
        noticeMessage.setGroupId(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 发布群公告
     *
     * @param user
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendNoticeMessage(Account user, String groupId, String notice) {
        if (Objects.isNull(user)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.NOTICE_OPERATION.getCode());
        noticeMessage.setMessage("“" + user.getNickname() + "”发布了群公告");
        noticeMessage.setGroupId(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        data.put("notice", notice);
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 取消群公告
     *
     * @param user
     * @param groupId
     */
    @Async("threadPoolTaskExecutor")
    public void sendCancelNoticeMessage(Account user, String groupId) {
        if (Objects.isNull(user)) {
            return;
        }
        if (StringUtils.isEmpty(groupId)) {
            return;
        }
        GroupNoticeMessage noticeMessage = new GroupNoticeMessage();
        noticeMessage.setOperatorUserId(user.getId().toString());
        noticeMessage.setOperation(GroupNoticeOperationEnum.CANCEL_NOTICE_OPERATION.getCode());
        noticeMessage.setMessage("“" + user.getNickname() + "”取消了群公告");
        noticeMessage.setGroupId(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("operatorNickname", user.getNickname());
        noticeMessage.setData(JSON.toJSONString(data));

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_MESSAGE.getCode());
        body.setSenderId(user.getId().toString());
        body.setTargetIds(new String[]{groupId});
        body.setJson(JSONObject.toJSONString(noticeMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + groupId);
        ProducerEvent.producerFifoMessage(message);
    }


}
